<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/1/9 0009
 * Time: 下午 3:05
 */

namespace App\Eloquent\Ygt;

use Illuminate\Database\Eloquent\SoftDeletes;

class Customer extends DbEloquent
{
    use SoftDeletes;
    protected $table = 'ygt_customer';
    protected $dates = ['deleted_at'];
    protected $dateFormat = 'U';
    public static $tableFieldList   = [
        ['name'=>'id','title'=>'','value'=>0,'is_must'=>0],
        ['name'=>'company_id','title'=>'','value'=>0,'is_must'=>1],
        ['name'=>'customer_name','title'=>'客户名称','value'=>'','is_must'=>1],
        ['name'=>'payment_days','title'=>'账期','value'=>'','is_must'=>0],
        ['name'=>'credit_amount','title'=>'信用金额','value'=>'','is_must'=>0],
        ['name'=>'sales_uid','title'=>'销售员','value'=>'','is_must'=>0],
        ['name'=>'contact_person','title'=>'客方代表','value'=>'','is_must'=>0],
        ['name'=>'contact_phone','title'=>'客方代表电话','value'=>'','is_must'=>0],
        ['name'=>'customer_tel','title'=>'客户电话','value'=>'','is_must'=>0],
        ['name'=>'short_customer_tel','title'=>'短号','value'=>'','is_must'=>0],
        ['name'=>'customer_address','title'=>'客户地址','value'=>'','is_must'=>0],
        ['name'=>'contact_address','title'=>'客方代表地址','value'=>'','is_must'=>0],
        ['name'=>'avatars_id','title'=>'头像','value'=>'','is_must'=>0],
        ['name'=>'customer_level','title'=>'客户等级','value'=>0,'is_must'=>0],
        ['name'=>'province_id','title'=>'省份','value'=>0,'is_must'=>0],
        ['name'=>'city_id','title'=>'城市','value'=>0,'is_must'=>0],
        ['name'=>'area_id','title'=>'地区','value'=>0,'is_must'=>0],
        ['name'=>'province_name','title'=>'省份','value'=>'','is_must'=>0],
        ['name'=>'city_name','title'=>'城市','value'=>'','is_must'=>0],
        ['name'=>'area_name','title'=>'地区','value'=>'','is_must'=>0],
        ['name'=>'first_letter','title'=>'首字母','value'=>'','is_must'=>1],
        ['name'=>'merchandiser_uid','title'=>'跟单员','value'=>'','is_must'=>0],
        ['name'=>'user_id','title'=>'关联账号','value'=>0,'is_must'=>0],
        ['name'=>'reg_mobile','title'=>'关联账号','value'=>0,'is_must'=>0],
        ['name'=>'merchandiser_name','title'=>'','value'=>'','is_must'=>0],
        ['name'=>'sales_name','title'=>'厂销售姓名','value'=>'','is_must'=>0],
        ['name'=>'sales_mobile','title'=>'厂销售电话','value'=>'','is_must'=>0],
        ['name'=>'add_user_id','title'=>'添加该数据的用户id','value'=>0,'is_must'=>0],
        ['name'=>'settlement_method','title'=>'结算方式','value'=>'','is_must'=>0],
        ['name'=>'is_add_order','title'=>'是否可以添加订单','value'=>'','is_must'=>0],
    ];

    public function sales(){
        return $this->hasOne('App\Eloquent\Ygt\DepartmentUser','id','sales_uid');
    }
    public function merchandiser(){
        return $this->hasOne('App\Eloquent\Ygt\DepartmentUser','id','merchandiser_uid');
    }

    public function buyers(){
        return $this->hasMany('App\Eloquent\Ygt\Buyers','id','customer_id');
    }

    public static function edit($data,$pk='')
    {
        $id                 = isset($data['id']) ? intval($data['id']) : 0;
        if($id>0){
            $where          = ['id'=>$id];
            $result         = self::updateOneData($where, $data,$pk);
        } else {
            $result         = self::insertOneData($data,$pk);
        }
        return $result;
    }
    public static function getCompanyList($whereArr)
    {
        $query              = self::query();
        $selectSql          = 'ygt_company.id,ygt_company.title,ygt_company.mobile';
        $selectSql          .= ',ygt_company.province_id,ygt_company.city_id';
        $selectSql          .= ',ygt_company.area_id,ygt_company.province_name';
        $selectSql          .= ',ygt_company.city_name,ygt_company.area_name';
        $selectSql          .= ',ygt_company.address,ygt_company.first_letter';
        $selectSql          .= ',ygt_customer.id as customer_id';
        $selectSql          .= ',ygt_customer.customer_name';
        $query              = $query->selectRaw($selectSql);
        if(isset($whereArr['user_id']))
        {
            $query          = $query->where('ygt_customer.user_id','=',$whereArr['user_id']);
        }
        if(isset($whereArr['title']))
        {
            $title          = '%'.$whereArr['title'].'%';
            $query          = $query->where('title','like',$title);
        }
        $query              = $query->leftJoin('ygt_company', 'ygt_company.id', '=', 'ygt_customer.company_id');
        $query              = $query->orderByDesc('ygt_company.id');
        $result             = $query->get();
        return $result;
    }
}